import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import javax.xml.transform.Result;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-12
 * Time: 20:50
 * author:JdbcSelect
 */
public class JdbcSelect {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource ) .setURL("jdbc:mysql://127.0.0.1:3306/java_104?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource ) .setUser("root");
        ((MysqlDataSource) dataSource ) .setPassword("123456");

        //2.创建连接
        Connection connection = dataSource.getConnection();



        //4. 操作数据库
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);

        //5.执行 查询操作,使用 executeQuery()方法 ,
        //使用 ResultSet 接收 返回的临时表
        ResultSet resultSet = statement.executeQuery();
        // 打印显示返回的表
        while( resultSet.next()) {
            //针对当前这一行来获取其中的类
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id = " + id + " ," + "name = " + name);
        }

        //6.关闭资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
